package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApUserFollowDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserFollowService;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 用户关注数据处理层实现类
 *
 * @author 11494
 * @version 1.0
 * @project heima-leadnews-group
 * @date 2024/1/26 10:59:53
 */
@Service
@Slf4j
@Transactional
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private CacheService cacheService;

    /**
     * 功能描述：关注或取消关注功能实现
     *
     * @param apUserFollowDto
     * @return {@link ResponseResult }
     * @author sungang
     * @date 2024/01/26  11:21
     */

    @Override
    public ResponseResult followOperation(ApUserFollowDto apUserFollowDto) {
        //参数校验
        if (apUserFollowDto.getAuthorId() == null || apUserFollowDto.getArticleId() == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //数据处理
        if (apUserFollowDto.getOperation() == 0) {
            cacheService.hPut("isFollow_" + AppThreadLocalUtil.getUser().getId(), apUserFollowDto.getAuthorId().toString(), apUserFollowDto.getArticleId());
            //关注
            ApUser apUser = apUserMapper.selectById(AppThreadLocalUtil.getUser().getId());
            if (apUser != null) {
                String userName = apUser.getName();
                ApUserFollow apUserFollow = new ApUserFollow();
                apUserFollow.setUserId(AppThreadLocalUtil.getUser().getId().longValue());
                apUserFollow.setFollowId(apUserFollowDto.getAuthorId());
                apUserFollow.setFollowName(userName);
                apUserFollow.setLevel(1);
                apUserFollow.setIsNotice(1);
                apUserFollow.setCreatedTime(new Date());
                save(apUserFollow);
                // redis
            }
        } else {
            cacheService.hDelete("isFollow_" + AppThreadLocalUtil.getUser().getId(), apUserFollowDto.getAuthorId().toString());
            //取消关注
            LambdaQueryWrapper<ApUserFollow> wrapper = Wrappers.<ApUserFollow>lambdaQuery()
                    .eq(apUserFollowDto.getAuthorId() != null &&
                            !Objects.equals("", apUserFollowDto.getAuthorId()), ApUserFollow::getFollowId, apUserFollowDto.getAuthorId())
                    .eq(ApUserFollow::getUserId, AppThreadLocalUtil.getUser().getId());
            remove(wrapper);
            //存储到redis中 被关注作者id,关注者id,文章id

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
